/**
 * 地址填写
 */
var R = false, A = false, P = false;

$(function() {
	$("#addressId").val(-1);
	var ops = $("#address");//下拉列表
	
	$.ajax({
		type:"POST",
		url:"order.do?method=2",
		dataType:"json",
		async:false,
		success:function(data){
			var addrs = data.addrs;
			appendOps(addrs);
			ops.change(function(){
				fillForm(addrs,$(this).val());
			});
		}
	});
	
	function appendOps(addrs) {
		for(var i =0 ;i<addrs.length; i++)
			{
				ops.append("<option value='" + i + "'>" + i + ":" + addrs[i].fullAddress.substr(0,6)+"...</option>");
			}
	}
	
	//填充表单
	function fillForm(addrs,index) {
		if(index==-1){
			$("#receiveName").val('');
			$("#fullAddress").val('');
			$("#postalCode").val('');
			$("#phone").val('');
			$("#mobile").val('');
			$("#addressId").val(-1);
			return ;
		}
		
		R = true, A = true, P = true;
		
		$("#nameValidMsg>span").html('');
		$("#addressValidMsg>span").html('');
		$("#receiveName").val(addrs[index].receiveName);
		$("#fullAddress").val(addrs[index].fullAddress);
		$("#postalCode").val(addrs[index].postalCode);
		$("#phone").val(addrs[index].phone);
		$("#mobile").val(addrs[index].mobile);
		$("#addressId").val(addrs[index].addressId);
	}

	//表单验证
	$("#receiveName").blur(function(){
		var val = $(this).val();
		
		if(val == ""){
			$("#nameValidMsg>span").text("不能为空");
		}else{
			R=true;
			$("#nameValidMsg>span").html("<img src='./images/my/ajax_ok.png'/>");
		}
			
	});
	
	$("#fullAddress").blur(function(){
		var val = $(this).val();
		
		if(val == ""){
			$("#addressValidMsg>span").text("不能为空");
		}else{
			A=true;
			$("#addressValidMsg>span").html("<img src='./images/my/ajax_ok.png'/>");
		}
	});
	
	$("#postalCode").blur(function(){
			var val = $(this).val();
			
			if(val == ""){
				$("#codeValidMsg>span").text("不能为空");
			}
			else{
				P=true;
				$("#codeValidMsg>span").html("<img src='./images/my/ajax_ok.png'/>");
			}
				
		});	
	$("#btnClientRegister").click(function(){
		if(R && A && P ){
			return true;
		}
		else{
			return false;
		}
	});
	
});